//
//  HomeTableViewController.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/5.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
import SVProgressHUD
let statusNormalIdentifier = "statusNormalIdentifier"
let statusRetweetedIdentifier = "statusRetweetedIdentifier"
class HomeTableViewController: VisitorTableViewController {
let pictureBrowerAnimator : PictureBrowerAnimator = PictureBrowerAnimator()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogon{
            visitorView?.setupInfo("关注一些人，回这里看看有什么惊喜", imageName: nil)
            return
        }
        perpareTableViewCell()
        loadDate()
        NSNotificationCenter.defaultCenter().addObserverForName(WBStatusSelectedPhotoNotification, object: nil, queue: nil) { (n) -> Void in
            guard let indexpath = n.userInfo![WBStatusSelectorPhotosIndexPathKey] as? NSIndexPath else{
                return
            }
            guard let urls = n.userInfo![WBStatusSelectorPhotosURLsKey] as? [NSURL] else{
                return
            }
            guard let collectionView = n.object as? PictureBrowerPresentDeldegate else{
                return
            }
            
            let vc = PictureBrowserViewController(thumbnailURLs: urls, indexPath: indexpath)
            vc.modalPresentationStyle = .Custom
            vc.transitioningDelegate = self.pictureBrowerAnimator
            self.pictureBrowerAnimator.setDelegateParam(collectionView, dismissDelegate: vc, indexPath: indexpath)
            self.presentViewController(vc, animated: true, completion: nil)
        }

    }
    var statusList = StatusListViewController()
    // MARK: - Table view data source
/// 下拉刷新指示器
    lazy var pullupView: UIActivityIndicatorView = {
       let pullup = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        pullup.color = UIColor.lightGrayColor()
        return pullup
    }()
    func perpareTableViewCell(){
        tableView.registerClass(StatusNomalCell.self, forCellReuseIdentifier: statusNormalIdentifier)
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: statusRetweetedIdentifier)
        tableView.estimatedRowHeight = 400
        tableView.separatorStyle = .None
        refreshControl = WBRefreshController()
        refreshControl?.addTarget(self, action: "loadDate", forControlEvents: UIControlEvents.ValueChanged)
        tableView.tableFooterView = pullupView
           }
    func loadDate(){
        refreshControl?.beginRefreshing()
           statusList.loadUserStatus (isPullup: pullupView.isAnimating()) { (isSuccessed) -> () in
            self.refreshControl?.endRefreshing()
            self.pullupView.stopAnimating()
            if !isSuccessed{
                SVProgressHUD.showInfoWithStatus("数据加载错误")
                return
            }
            self.tableView.reloadData()
        }
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return statusList.statusList.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let vm = statusList.statusList[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(vm.statusID, forIndexPath:indexPath) as? StatusTableViewCell
        cell?.statusView = vm
        if indexPath.row == (statusList.statusList.count-1) && !pullupView.isAnimating(){
            pullupView.startAnimating()
            loadDate()
        }
        return cell!

    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return statusList.statusList[indexPath.item].rowHeight
    }
 
}
